package com.hz.us.mapper;

import com.hz.us.model.entity.UserEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select * from users")
    List<UserEntity> allUsers();

    @Insert("insert into users values (null, #{username},#{realname},#{student_id},#{pwd_salt},#{pwd},#{pwd_valid_date},#{user_type},#{pwd_error_count},#{class_id},#{grade_id})")
    int addUser(UserEntity userEntity);

    @Delete("delete from users where id = #{userId}")
    int delUser(@Param("id") Integer userId);

    @Select("select * from users where id = #{id}")
    UserEntity selectById(@Param("id") Integer userId);

    @Select("select * from users where username = #{username}")
    UserEntity selectByUsername(@Param("username") String username);

    @Update("update users set pwd = #{pwd}, pwd_valid_date = #{validDate} where id = #{id}")
    int updatePwd(@Param("id") Integer userId, @Param("pwd") String pwd, @Param("validDate") long validDate);

    @Update("update users set pwd_error_count = #{errorCount} where id = #{userId}")
    int updatePwdErrorCount(Integer userId, Integer errorCount);

    @Select("select * from users where username = #{username} and user_type = #{userType}")
    UserEntity selectByUsernameAndUsertype(@Param("username") String username, @Param("userType") int userType);

    @Select("select * from users where user_type = 2")
    List<UserEntity> getAllTeachers();

    @Select("select * from users where user_type = 2 and id = #{id}")
    UserEntity selectTeacherById(@Param("id") Integer id);

    @Select("select * from users where user_type = 3 and id = #{id}")
    UserEntity selectStudentById(@Param("id") Integer id);

}
